{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85070fc5-7bb8-4439-9f9b-cc2044ea37b3",
   "metadata": {},
   "source": [
    "# 启动WebUI\n",
    "\n",
    "同济子豪兄 2024-6-12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c83d4dcf-37b5-46a0-bda8-38db8ae83d6c",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "91d49e64-2e18-4d24-aa51-7c271b0f1724",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b98b7e0-a363-4c2b-a2df-64554128bd5e",
   "metadata": {},
   "source": [
    "## 进入ChatTTS主目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "efe10618-bc86-4227-8fd2-1e5902ebc909",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir('ChatTTS')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8db94d89-481c-41dd-aa2b-4979c6a20ccb",
   "metadata": {},
   "source": [
    "## 修改`ChatTTS/webui.py`文件\n",
    "\n",
    "将第61行修改为自己的默认语句\n",
    "\n",
    "将第107行加上`compile=False`参数\n",
    "```python\n",
    "chat.load_models('local', local_path=args.local_path, compile=False)\n",
    "```\n",
    "\n",
    "将第109行加上`share=True`参数\n",
    "```python\n",
    "demo.launch(server_name=args.server_name, server_port=args.server_port, inbrowser=True, share=True)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e329dd3e-0293-4ee3-ba1d-9855c96b8354",
   "metadata": {},
   "source": [
    "## 启动WebUI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5227940f-7446-4b6a-94b4-945c9b8411a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading ChatTTS model...\n",
      "INFO:ChatTTS.core:Load from cache: /root/.cache/huggingface/hub/models--2Noise--ChatTTS/snapshots/cc14302f34d7855eb3420d1fd48345012ff1460d\n",
      "INFO:ChatTTS.core:use cuda:0\n",
      "IMPORTANT: You are using gradio version 4.7.1, however version 4.29.0 is available, please upgrade.\n",
      "--------\n",
      "INFO:ChatTTS.core:vocos loaded.\n",
      "INFO:ChatTTS.core:dvae loaded.\n",
      "INFO:ChatTTS.core:gpt loaded.\n",
      "INFO:ChatTTS.core:decoder loaded.\n",
      "INFO:ChatTTS.core:tokenizer loaded.\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "Running on local URL:  http://0.0.0.0:8080\n",
      "Running on public URL: https://52cd453a2696edb442.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "  0%|                                                   | 0/384 [00:00<?, ?it/s]/root/miniconda3/lib/python3.10/site-packages/torch/overrides.py:110: UserWarning: 'has_cuda' is deprecated, please use 'torch.backends.cuda.is_built()'\n",
      "  torch.has_cuda,\n",
      "/root/miniconda3/lib/python3.10/site-packages/torch/overrides.py:111: UserWarning: 'has_cudnn' is deprecated, please use 'torch.backends.cudnn.is_available()'\n",
      "  torch.has_cudnn,\n",
      "/root/miniconda3/lib/python3.10/site-packages/torch/overrides.py:117: UserWarning: 'has_mps' is deprecated, please use 'torch.backends.mps.is_built()'\n",
      "  torch.has_mps,\n",
      "/root/miniconda3/lib/python3.10/site-packages/torch/overrides.py:118: UserWarning: 'has_mkldnn' is deprecated, please use 'torch.backends.mkldnn.is_available()'\n",
      "  torch.has_mkldnn,\n",
      "/root/miniconda3/lib/python3.10/site-packages/torch/_inductor/compile_fx.py:135: UserWarning: TensorFloat32 tensor cores for float32 matrix multiplication available but not enabled. Consider setting `torch.set_float32_matmul_precision('high')` for better performance.\n",
      "  warnings.warn(\n",
      "  8%|███▏                                      | 29/384 [01:47<21:54,  3.70s/it]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 17%|██████▍                                | 340/2048 [00:02<00:12, 135.65it/s]\n",
      "/root/miniconda3/lib/python3.10/site-packages/gradio/processing_utils.py:354: UserWarning: Trying to convert audio automatically from float32 to 16-bit int format.\n",
      "  warnings.warn(warning.format(data.dtype))\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 17%|██████▉                                  | 65/384 [00:00<00:02, 151.03it/s]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 30%|███████████▉                            | 611/2048 [00:36<01:24, 16.92it/s]\n",
      "/root/miniconda3/lib/python3.10/site-packages/gradio/processing_utils.py:354: UserWarning: Trying to convert audio automatically from float32 to 16-bit int format.\n",
      "  warnings.warn(warning.format(data.dtype))\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 15%|█████▉                                 | 315/2048 [00:02<00:12, 141.79it/s]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 17%|██████▉                                  | 65/384 [00:00<00:02, 139.81it/s]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 30%|███████████▋                           | 611/2048 [00:05<00:11, 120.20it/s]\n"
     ]
    }
   ],
   "source": [
    "!python webui.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "298f73c6-39cf-4881-b6f7-390c90d2c28b",
   "metadata": {},
   "source": [
    "## 启动WebUI（趋动云）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "213d93c6-1d83-40de-9dad-d4922046043a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading ChatTTS model...\n",
      "local model path: /gemini/pretrain\n",
      "INFO:ChatTTS.core:Load from local: /gemini/pretrain\n",
      "INFO:ChatTTS.core:use cuda:0\n",
      "IMPORTANT: You are using gradio version 4.7.1, however version 4.29.0 is available, please upgrade.\n",
      "--------\n",
      "INFO:ChatTTS.core:vocos loaded.\n",
      "INFO:ChatTTS.core:dvae loaded.\n",
      "INFO:ChatTTS.core:gpt loaded.\n",
      "INFO:ChatTTS.core:decoder loaded.\n",
      "INFO:ChatTTS.core:tokenizer loaded.\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "Running on local URL:  http://0.0.0.0:8080\n",
      "Running on public URL: https://3acd4eb4d6f8309554.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n",
      "\u001b[33mWARNING\u001b[0m:  Invalid HTTP request received.\n",
      "\u001b[33mWARNING\u001b[0m:  Invalid HTTP request received.\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "WARNING:ChatTTS.core:Invalid characters found! : {'！'}\n",
      " 16%|██████▉                                   | 63/384 [00:02<00:14, 22.44it/s]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      " 27%|██████████▋                             | 545/2048 [00:09<00:27, 55.43it/s]\n",
      "/root/miniconda3/lib/python3.10/site-packages/gradio/processing_utils.py:354: UserWarning: Trying to convert audio automatically from float32 to 16-bit int format.\n",
      "  warnings.warn(warning.format(data.dtype))\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "WARNING:ChatTTS.core:Invalid characters found! : {'！'}\n",
      " 24%|█████████▌                              | 487/2048 [00:08<00:27, 55.85it/s]\n",
      "INFO:ChatTTS.core:All initialized.\n",
      "WARNING:ChatTTS.core:Invalid characters found! : {'！'}\n",
      " 24%|█████████▌                              | 487/2048 [00:08<00:27, 56.31it/s]\n"
     ]
    }
   ],
   "source": [
    "!python webui.py --local_path=/gemini/pretrain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "947a474f-155f-4b14-aa06-0458611f5087",
   "metadata": {},
   "source": [
    "## 全世界任意一个浏览器都可以访问这个网站，生成语音啦~\n",
    "\n",
    "```\n",
    "Running on public URL: https://52cd453a2696edb442.gradio.live\n",
    "```\n",
    "\n",
    "第一次生成时，需要几分钟初始化。\n",
    "\n",
    "这个公开链接不宜公开分享，以免显存被大量用户占用，而且这个链接会在72小时后过期"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8b49605-c545-42bb-bc8f-65df31155acb",
   "metadata": {},
   "source": [
    "## 更多参考资料\n",
    "\n",
    "WebUI界面：https://github.com/2noise/ChatTTS/issues/278\n",
    "\n",
    "音色试听：https://github.com/kangyiwen/TTSlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe4e4ab-aa7d-470a-8a0c-45ca8edd69d6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
